package zzs.generalframework.util.core.uuid;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import zzs.generalframework.util.core.calendar.CalendarUtils;

/**
 * @title
 * @description
 * @project zzs-util
 * @package zzs.generalframework.util.core.uuid
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class MD5Utils
{
	public static MD5Utils md5Utils = null;

	private MD5Utils()
	{

	}

	public static MD5Utils getInstance()
	{
		if (md5Utils == null)
		{
			md5Utils = new MD5Utils();
		}
		return md5Utils;
	}

	public String getMD5(String inputStr) throws NoSuchAlgorithmException
	{
		String returnString = null;
		StringBuffer resultBuffer = new StringBuffer();

		if (inputStr == null)
		{
			inputStr = getDefaultString();
		}

		MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		messageDigest.update(inputStr.getBytes());
		byte[] MD5ByteArray = messageDigest.digest();
		for (int pos = 0; pos < MD5ByteArray.length; pos++)
		{
			int value = MD5ByteArray[pos];
			if (value < 0)
			{
				value += 256;
			}
			if (value < 16)
			{
				resultBuffer.append("0");
			}
			resultBuffer.append(Integer.toHexString(value).toUpperCase());
		}
		returnString = resultBuffer.toString();
		return returnString;
	}

	private String getDefaultString()
	{
		String returnString = null;
		StringBuffer resultBuffer = new StringBuffer();
		String systemDateTime = CalendarUtils.getSystemDateTime();
		resultBuffer.append(systemDateTime);
		returnString = resultBuffer.toString();
		return returnString;
	}

}
